import time

import pyautogui
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from head.Init_Driver import SurveyExaminationSystem_Driver


class SystemManagement:
    url = ""
    driver = ""
    def __init__(self):
        # 集成测试
        self.url = "http://8.155.1.153:8081/system/user"
        self.driver = SurveyExaminationSystem_Driver.driver
        self.driver.get(self.url)
        self.driver.maximize_window()

        # 单元测试
        # self.url = "http://8.155.1.153:8081/user/login"
        # self.driver = SurveyExaminationSystem_Driver.driver
        # self.driver.get(self.url)
        # # 初始化登录成功状态
        # self.driver.find_element(By.ID, "username").send_keys("karen")
        # self.driver.find_element(By.ID, "password").send_keys("karen1234")
        # self.driver.find_element(By.CSS_SELECTOR,"#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button").click()
        # # 检查是否登录成功
        # # assert WebDriverWait(self.driver, 3).until(expected_conditions.visibility_of_element_located((By.XPATH,"//a[h1='问卷系统']")))
        # assert WebDriverWait(self.driver, 3).until(
        #     expected_conditions.visibility_of_element_located(
        #         (By.CSS_SELECTOR,"#sk-layout > div > div > section > div > header > div > div.ant-pro-global-header-right-content > div > div > div")))
        # # 点击“模板广场”进入相关页面
        # self.driver.find_element(By.XPATH, "//span[text()='系统管理']").click()

    def UserManagementUIFuncTest(self):
        # 点击“用户管理”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='用户管理']")))
        self.driver.find_element(By.XPATH,"//li[span='用户管理']").click()
        # 测试“姓名”输入框和“查询”按钮功能是否正常
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='姓名']")))
        self.driver.find_element(By.ID,"name").send_keys("Admin")
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        time.sleep(1)
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 点击“新建”按钮
        self.driver.find_element(By.XPATH,"//span[text()='新建']").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='新建用户']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()

        # 测试题库列表中“批量导入”按钮是否正常
        # assert WebDriverWait(self.driver,3).until(
        #     expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='批量导入']")))
        # self.driver.find_element(By.XPATH,"//span[text()='批量导入']").click()
        # time.sleep(1)
        # # 检查是否跳转至相关页面
        # assert WebDriverWait(self.driver,3).until(
        #     expected_conditions.visibility_of_element_located((By.XPATH,"//div[p='点击或者拖拽Excel文件到此处']")))
        # self.driver.find_element(By.XPATH,"//div[p='点击或者拖拽Excel文件到此处']").click()
        # pyautogui.sleep(1) # 在输入文件名称之前最好搞个强制等待，亲测不然会输入乱序
        # filepath = "D:\Desktop\TestFile.xlsx"
        # pyautogui.write(filepath,interval=0.1)
        # pyautogui.sleep(1)
        # pyautogui.press('enter')
        # # pyautogui.sleep(1)
        # # 获取屏幕分辨率
        # print(f'当前屏幕分辨率为：{pyautogui.size()}')
        # # 获取鼠标要点击的地方
        # print(f'鼠标需要移动到：{pyautogui.position()}')
        # # 移动要点击的地方，在一秒内
        # pyautogui.moveTo(940,840,duration=0.5)
        # # # 单击左键
        # pyautogui.click(button='left')
        # # # 点击导入上传
        # assert WebDriverWait(self.driver,3).until(
        #     expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='导入']")))
        # self.driver.find_element(By.XPATH, "//span[text()='导入']").click()
        # time.sleep(1)

        # 点击“编辑”按钮
        self.driver.find_element(By.LINK_TEXT,"编辑").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='修改用户']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()

    def RoleManagementUIFuncTest(self):
        # 点击“角色管理”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='角色管理']")))
        self.driver.find_element(By.XPATH,"//li[span='角色管理']").click()
        # 测试“角色名称”输入框和“查询”按钮功能是否正常
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='角色名称']")))
        self.driver.find_element(By.ID,"name").send_keys("karen")
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        time.sleep(1)
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 点击“新建”按钮
        self.driver.find_element(By.XPATH,"//span[text()='新建']").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='查看角色']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()

        # 点击“编辑”按钮
        self.driver.find_element(By.LINK_TEXT,"编辑").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='修改角色']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()

    def OrganizationUIFuncTest(self):
        # 点击“组织机构”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='组织机构']")))
        self.driver.find_element(By.XPATH,"//li[span='组织机构']").click()
        # 点击“新建”按钮
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='新建']")))
        self.driver.find_element(By.XPATH,"//span[text()='新建']").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='新建组织机构']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()
        # 点击“编辑”按钮
        try:
            self.driver.find_element(By.LINK_TEXT,"编辑").click()
            # 检测是否跳转至相关页面
            assert WebDriverWait(self.driver,3).until(
                expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='修改组织机构']")))
            time.sleep(1)
            self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()
        except:
            print('succced')

    def JobSettingUIFuncTest(self):
        # 点击“岗位设置”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='岗位设置']")))
        self.driver.find_element(By.XPATH,"//li[span='岗位设置']").click()
        # 测试“名称”输入框和“查询”按钮功能是否正常
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='名称']")))
        self.driver.find_element(By.ID,"name").send_keys("前端开发岗位")
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        time.sleep(1)
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 点击“新建”按钮
        self.driver.find_element(By.XPATH,"//span[text()='新建']").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='查看岗位']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()

        # 点击“编辑”按钮
        self.driver.find_element(By.LINK_TEXT,"编辑").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='修改岗位']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()

    def DictionaryManagementUIFuncTest(self):
        # 点击“字典管理”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='字典管理']")))
        self.driver.find_element(By.XPATH,"//li[span='字典管理']").click()
        # 测试“字典名称”输入框和“查询”按钮功能是否正常
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='字典名称']")))
        self.driver.find_element(By.ID,"name").send_keys("行业字典")
        self.driver.find_element(By.XPATH,"//button[span='查 询']").click()
        time.sleep(1)
        # 点击“重置”按钮
        self.driver.find_element(By.XPATH,"//button[span='重 置']").click()
        # 点击“新建”按钮
        self.driver.find_element(By.XPATH,"//span[text()='新建']").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='新增字典']")))
        time.sleep(1)
        self.driver.find_element(By.XPATH,"//span[text()='取 消']").click()

        # 点击“字典项”按钮
        self.driver.find_element(By.LINK_TEXT,"字典项").click()
        # 检测是否跳转至相关页面
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//div[text()='字典列表']")))
        time.sleep(1)
        self.driver.find_element(By.CSS_SELECTOR,"span[aria-label='close']").click()

    def SettingsUIFuncTest(self):
        # 基本设置
        # 点击“个人设置”
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//span[text()='个人设置']")))
        self.driver.find_element(By.XPATH,"//li[span='个人设置']").click()
        # 测试“姓名，手机号，邮箱，个人简介”等输入框
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='姓名']")))
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='手机号']")))
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='邮箱']")))
        assert WebDriverWait(self.driver,3).until(
            expected_conditions.visibility_of_element_located((By.XPATH,"//label[text()='个人简介']")))
        self.driver.find_element(By.ID,"name").clear()
        self.driver.find_element(By.ID,"phone").clear()
        self.driver.find_element(By.ID,"email").clear()
        self.driver.find_element(By.ID,"profile").clear()
        self.driver.find_element(By.ID,"name").send_keys("karensouza")
        self.driver.find_element(By.ID,"phone").send_keys("15599999999")
        self.driver.find_element(By.ID,"email").send_keys("karensouza")
        self.driver.find_element(By.ID,"profile").send_keys("贼帅气的一羞涩男孩")
        self.driver.find_element(By.XPATH,"//span[text()='提 交']").click()
        time.sleep(1)
        # 测试头像是否可以正常更换
        self.driver.find_element(By.XPATH,"//span[text()='更换头像']").click()
        # 启动安全设置
        pyautogui.PAUSE = 1
        pyautogui.FAILSAFE = True
        time.sleep(1)
        pyautogui.write("D:\Desktop\prettyDog.jpg",interval=0.1)
        pyautogui.press('enter')
        # 获取鼠标要点击的地方
        print(f'鼠标需要移动到：{pyautogui.position()}')
        pyautogui.moveTo(920,840,duration=0.5)
        # 点击文件对话框“打开”按钮
        pyautogui.click(button = 'left')
        print(f'鼠标需要移动到：{pyautogui.position()}')
        pyautogui.moveTo(1515,1193,duration=0.5)
        # 点击“放大”按钮
        pyautogui.click(button = 'left')
        print(f'鼠标需要移动到：{pyautogui.position()}')
        pyautogui.moveTo(1015,1258,duration=0.5)
        # 点击“左旋”按钮
        pyautogui.click(button = 'left')
        pyautogui.click(button = 'left')
        pyautogui.click(button = 'left')
        print(f'鼠标需要移动到：{pyautogui.position()}')
        pyautogui.moveTo(1690,1420,duration=0.5)
        # 点击“确定”按钮
        pyautogui.click(button = 'left')
        time.sleep(2)


# systemManagement = SystemManagement()
# systemManagement.UserManagementUIFuncTest()
# systemManagement.RoleManagementUIFuncTest()
# systemManagement.OrganizationUIFuncTest()
# systemManagement.JobSettingUIFuncTest()
# systemManagement.DictionaryManagementUIFuncTest()
# systemManagement.SettingsUIFuncTest()
# systemManagement.driver.quit()